package com.longxi.tianjintianbao.ui.presenter.visitor;

import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.MyApplication;
import com.longxi.tianjintianbao.R;
import com.longxi.tianjintianbao.api.ApiRetrofit;
import com.longxi.tianjintianbao.api.response.BaseResponse;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.exception.ExceptionHandle;
import com.longxi.tianjintianbao.exception.MySubscriber;
import com.longxi.tianjintianbao.model.visitor.CertificateType;
import com.longxi.tianjintianbao.model.visitor.Visitor;
import com.longxi.tianjintianbao.ui.activity.repair_publish.SelectAreaActivity;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.presenter.repair_publish.SelectAreaAtPresenter;
import com.longxi.tianjintianbao.ui.view.visitor.IVisitorDetailAtView;
import com.longxi.tianjintianbao.utils.MyUtils;
import com.longxi.tianjintianbao.utils.TimeComparisonUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class VisitorDetailAtPresenter extends BasePresenter<IVisitorDetailAtView> {

    public static String VISITOR = "VISITOR";//访客登记详情

    private String certificatetypeid = "";
    private String houseid = "";
    private Visitor visitor;

    public VisitorDetailAtPresenter(BaseActivity context) {
        super(context);
    }

    public void setBar() {
        Intent intent = mContext.getIntent();
        visitor = (Visitor) intent.getSerializableExtra(VISITOR);
        mContext.setTitle("访客登记");
        mContext.setTvLeft(MyUtils.getString(R.string.back));

        if(visitor == null){
            return;
        }
        mContext.setTitle("访客详情");
        if("".equals(visitor.getVisitdate())){
            getView().getBtnLogin().setText("访客来访登记");
        }else{
            getView().getBtnLogin().setVisibility(View.GONE);
        }
        setDetail();
    }

    private void setDetail() {
        StringBuilder buffer = new StringBuilder();

        if(!TextUtils.isEmpty(visitor.getBillno())){
            buffer.append("单据编号:");
            buffer.append(visitor.getBillno());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(visitor.getBuild())){
            buffer.append("所属项目:");
            buffer.append(visitor.getBuild());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(visitor.getVisitorname())){
            buffer.append("来  访  人:");
            buffer.append(visitor.getVisitorname());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(visitor.getVisitorphone())){
            buffer.append("来访人电话:");
            buffer.append(visitor.getVisitorphone());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(visitor.getReceivedate())){
            buffer.append("预约来访时间:");
            buffer.append(visitor.getReceivedate());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(visitor.getVisitortype())){
            buffer.append("受访人性质:");
            buffer.append(visitor.getVisitortype());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(visitor.getMaster())){
            buffer.append("受访人姓名:");
            buffer.append(visitor.getMaster());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(visitor.getMasterphone())){
            buffer.append("受访人电话:");
            buffer.append(visitor.getMasterphone());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(visitor.getHousedept())){
            buffer.append("受访房号部门:");
            buffer.append(visitor.getHousedept());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(visitor.getCause())){
            buffer.append("来访事由:");
            buffer.append(visitor.getCause());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(visitor.getRemarks())){
            buffer.append("备        注:");
            buffer.append(visitor.getRemarks());
            buffer.append("\n");
        }

        if(!TextUtils.isEmpty(visitor.getVisitdate())){
            buffer.append("来访时间:");
            buffer.append(visitor.getVisitdate());
            buffer.append("\n");
        }

        getView().getTvContent().setText(buffer);
    }

    public void btnLogin(){
        if(visitor == null){
            saveVisitor();
        }else{
            saveVisitorLeave();
        }
    }

//    private void saveVisitorLeave() {
//        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
//        JSONObject object = new JSONObject();
//        try {
//            object.put("userid", MyApplication.loginUser.userid);
//            object.put("billid", visitor.getBillid());
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//
//        ApiRetrofit.getInstance().saveVisitorLeave(object)
//        .subscribeOn(Schedulers.io())
//        .observeOn(AndroidSchedulers.mainThread())
//        .subscribe(new MySubscriber<BaseResponse>(mContext) {
//            @Override
//            public void onError(ExceptionHandle.ResponeThrowable throwable) {
//                mContext.hideWaitingDialog();
//                MyUtils.showToast(throwable.message);
//            }
//
//            @Override
//            public void onNext(BaseResponse baseResponse) {
//                mContext.hideWaitingDialog();
//                if(baseResponse.getCode() != 0){
//                    MyUtils.showToast(baseResponse.getMsg());
//                    return;
//                }
//                MyUtils.showToast("提交成功");
//                mContext.setResult(mContext.RESULT_OK);
//                mContext.finish();
//            }
//        });
//    }

    public void saveVisitorLeave() {
        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
        JSONObject object = new JSONObject();
        try {
            object.put("billid", visitor.getBillid());
            object.put("userid", MyApplication.loginUser.userid);
            object.put("build", visitor.getBuild());
            object.put("buildid", visitor.getBuildid());
            object.put("visitorname", visitor.getVisitorname());
            object.put("visitorphone", visitor.getVisitorphone());
            object.put("receivedate", visitor.getReceivedate());
            object.put("visitortype", visitor.getVisitortype());
            object.put("master", visitor.getMaster());
            object.put("masterphone", visitor.getMasterphone());
            object.put("housedept", visitor.getHousedept());
            object.put("cause", visitor.getCause());
            object.put("visitdate", TimeComparisonUtils.getDate());
            object.put("remarks", visitor.getRemarks());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ApiRetrofit.getInstance().saveVisitor(object)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new MySubscriber<BaseResponse>(mContext) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable throwable) {
                        mContext.hideWaitingDialog();
                        MyUtils.showToast(throwable.message);
                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {
                        mContext.hideWaitingDialog();
                        if(baseResponse.getCode() != 0){
                            MyUtils.showToast(baseResponse.getMsg());
                            return;
                        }
                        MyUtils.showToast("提交成功");
                        mContext.setResult(mContext.RESULT_OK);
                        mContext.finish();
                    }
                });
    }

    public void saveVisitor() {
//        long userid = MyApplication.loginUser.userid;
//        String visitorname = getEditText(getView().getEtVisitorName());
//        String visitorphone = getEditText(getView().getEtVisitorPhone());
//        String certificateno = getEditText(getView().getEtCertificateNo());
//        String cause = getEditText(getView().getEtCause());
//        String master = getEditText(getView().getEtMaster());
//        String masterphone = getEditText(getView().getEtMasterPhone());
//        String remarks = getEditText(getView().getEtRemarks());
//
//        if("".equals(visitorname) || visitorname == null){
//            MyUtils.showToast(getView().getEtVisitorName().getHint().toString());
//            return;
//        }
//
//        if("".equals(visitorphone) || visitorphone == null){
//            MyUtils.showToast(getView().getEtVisitorPhone().getHint().toString());
//            return;
//        }
//
//        if("".equals(certificatetypeid) || certificatetypeid == null){
//            MyUtils.showToast("请选择证件类型");
//            return;
//        }
//
//        if("".equals(certificateno) || certificateno == null){
//            MyUtils.showToast("请输入证件号");
//            return;
//        }
//
//        if("".equals(cause) || cause == null){
//            MyUtils.showToast("来访事由");
//            return;
//        }
//
//        if("".equals(houseid) || houseid == null){
//            MyUtils.showToast("请选择拜访房间");
//            return;
//        }
//
//        mContext.showWaitingDialog(MyUtils.getString(R.string.load));
//        JSONObject object = new JSONObject();
//        try {
//            object.put("userid", userid);
//            object.put("visitorname", visitorname);
//            object.put("visitorphone", visitorphone);
//            object.put("certificatetypeid", certificatetypeid);
//            object.put("certificateno", certificateno);
//            object.put("cause", cause);
//            object.put("houseid", houseid);
//            object.put("master", master);
//            object.put("masterphone", masterphone);
//            object.put("remarks", remarks);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//
//        ApiRetrofit.getInstance().saveVisitor(object)
//        .subscribeOn(Schedulers.io())
//        .observeOn(AndroidSchedulers.mainThread())
//        .subscribe(new MySubscriber<BaseResponse>(mContext) {
//            @Override
//            public void onError(ExceptionHandle.ResponeThrowable throwable) {
//                mContext.hideWaitingDialog();
//                MyUtils.showToast(throwable.message);
//            }
//
//            @Override
//            public void onNext(BaseResponse baseResponse) {
//                mContext.hideWaitingDialog();
//                if(baseResponse.getCode() != 0){
//                    MyUtils.showToast(baseResponse.getMsg());
//                    return;
//                }
//                MyUtils.showToast("提交成功");
//                mContext.setResult(mContext.RESULT_OK);
//                mContext.finish();
//            }
//        });
    }

    /**
     * 选择小区地址
     */
    public void selectArea() {
        Intent mainIntent = new Intent(mContext, SelectAreaActivity.class);
        mainIntent.putExtra(SelectAreaAtPresenter.REPAIRAREA,"0");
        mContext.startActivityForResult(mainIntent, AppConst.IntentRequstCode.ACTIVITY_REPAIR_CONTENT);
    }

    public String getEditText(EditText editText){
        return editText.getText().toString().trim();
    }

}
